package utsc.apps.utschedule.util.provider;

import utsc.apps.utschedule.classes.Course;
import utsc.apps.utschedule.classes.Timetable;
import utsc.apps.utschedule.util.database.DatabaseManager;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class CourseParentProvider extends ContentProvider {

	public static final String TAG="utsc.apps.utschedule.util.provider.CourseParentProvider";
	public static final String AUTHORITY="utsc.apps.utschedule.util.provider.CourseParentProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/search");
	public static final int COURSES=1;
	public static final int COURSE=2;
	public static final int SEARCH_SUGGEST=99;
	
	private static final UriMatcher _URI_MATCHER =buildUriMatcher();
	
	// MIME types used for searching
    public static final String COURSES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE +
                                                  "/vnd.utsc.apps.utschedule.courses";
    public static final String COURSE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE+
            "/vnd.utsc.apps.utschedule.courses";
    
	/**
     * Builds up a UriMatcher for search suggestion and shortcut refresh queries.
     */
    private static UriMatcher buildUriMatcher() {
        UriMatcher matcher =  new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(AUTHORITY, "courses", COURSES);
        matcher.addURI(AUTHORITY, "courses/#", COURSE);
        
      
        // to get suggestions...
        matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
        matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);

        return matcher;
    }
	
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		switch (_URI_MATCHER.match(uri)) {
		case COURSES:
			return COURSES_MIME_TYPE;
		case COURSE:
			return COURSE_MIME_TYPE;
		
		case SEARCH_SUGGEST:
			return SearchManager.SUGGEST_MIME_TYPE;
		/*case ONLINE:
			return ONLINE_MIME_TYPE;*/
		default:
			throw new IllegalArgumentException("Unknown URL "+uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		  if (selectionArgs == null) {
            throw new IllegalArgumentException(
                "selectionArgs must be provided for the Uri: " + uri);
          }
          return getSuggestions(selectionArgs[0]);
	}

	private static final String[] PARENT_COURSE_COLS={Course.COURSE_ID+" as _id ",Course.COURSE_CODE};
  	
	private Cursor getSuggestions(String query) {
	     
	      Log.d(TAG, query);
	      final DatabaseManager dbManager=DatabaseManager.getInstance(getContext());
	      Timetable t=dbManager.getCurrentActiveTimetable();
	      int timetableID=0;
	      
	      if (t!=null) {
	    	  timetableID=t.get_timetableID();
	      }
	      
	      return dbManager.queryParentCourse(query, timetableID, PARENT_COURSE_COLS);
	}
	
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean onCreate() {
		return true;
	}

}
